﻿/*********************************************************
 * 开发人员：小兵
 * 创建时间：2012-10-24 15:39:14
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/
namespace Mozlite.WebUI.Mvc4.Backend.Controllers
{
    using System;
    using System.Web;
    using System.Data;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Mozlite.Components;
    using System.Web.Mvc;
    using System.Text.RegularExpressions;
    using Storages = Mozlite.Components.FileStorages;

    /// <summary>
    /// 用户中心。
    /// </summary>
    [AuthorizeEx]
    public class UCenterController : BaseController
    {
        #region users
        /// <summary>
        /// 编辑用户档案。
        /// </summary>
        /// <returns>返回试图结果。</returns>
        public ActionResult EditProfile() {
            return View(Self);
        }

        /// <summary>
        /// 编辑用户档案。
        /// </summary>
        /// <param name="model">用户模型。</param>
        /// <returns>返回试图结果。</returns>
        [HttpPost]
        public ActionResult EditProfile(User model)
        {
            if(string.IsNullOrEmpty(model.Email))
            {
                ShowLocalError(CreateUserStatus.EmailNotNull);
                return View(model);
            }
            model.Email = model.Email.Trim();
            if(!model.Email.IsEmail())
            {
                ShowLocalError(CreateUserStatus.EmailInvalid);
                return View(model);
            }
            //需要将用户实例附加到当前模型
            model.UserID = Self.UserID;
            model.UserName = Self.UserName;

            var status = Users.EditProfile(model);
            if(status == CreateUserStatus.Updated)
                ShowLocalSuccess(status);
            else
                ShowLocalError(status);
            return View(model);
        }

        /// <summary>
        /// 验证显示名称。
        /// </summary>
        /// <param name="displayName">显示名称。</param>
        /// <returns>返回验证结果。</returns>
        public ActionResult CheckDisplayName(string displayName)
        {
            if(Components.DisallowNames.Current.IsDeny(displayName))
                return Json(Local(CreateUserStatus.DisplayNameIsDeny), JsonRequestBehavior.AllowGet);
            if(Users.IsDisplayNameAvailable(Self.UserID, displayName))
                return Json(true, JsonRequestBehavior.AllowGet);
            return Json(Local(CreateUserStatus.DuplicateDisplayName), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 修改密码。
        /// </summary>
        /// <returns>返回试图结果。</returns>
        public ActionResult ChangePassword()
        {
            return View();
        }

        /// <summary>
        /// 修改密码。
        /// </summary>
        /// <param name="form">表单数据。</param>
        /// <returns>返回试图结果。</returns>
        [HttpPost]
        public ActionResult ChangePassword(FormCollection form)
        {
            var src = form.Get("SrcPassword").NullSafeTrim();
            if(src == null)
            {
                ShowLocalError("CreateUserStatus_SrcPasswordNotNull");
                return View();
            }
            var password = form.Get("NewPassword").NullSafeTrim();
            if(password == null)
            {
                ShowLocalError("CreateUserStatus_NewPasswordNotNull");
                return View();
            }
            if(!Regex.IsMatch(password, Consts.PasswordRegex, RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline))
            {
                ShowLocalError(CreateUserStatus.PasswordInvalid);
                return View();
            }
            if(!password.IsCaseSensitiveEqual(form["NewConfirmPassword"].NullSafeTrim()))
            {
                ShowLocalError("CreateUserStatus_NewPasswordNotEqualConfirm");
                return View();
            }
            if(!Users.IsValid(src)){
                ShowLocalError("CreateUserStatus_SrcPasswordError");
                return View();
            }
            if(Users.ChangePassword(password))
                ShowLocalSuccess("ChangePassword_Success");
            else
                ShowLocalError("ChangePassword_Error");
            return View();
        }
        #endregion

        #region filestorages
        /// <summary>
        /// 文件存储试图。
        /// </summary>
        /// <param name="folderID">当前文件夹ID。</param>
        /// <returns>返回试图结果。</returns>
        public ActionResult FileStorages(int folderID = 0) {
            var folder = Storages.GetFolder(folderID, Self.UserID);
            return View(folder);
        }

        /// <summary>
        /// 文件存储试图。
        /// </summary>
        /// <param name="folderID">当前文件夹ID。</param>
        /// <returns>返回试图结果。</returns>
        [HttpPost]
        public ActionResult QueryStorages(int folderID = 0)
        {
            var directory = Storages.GetFolder(folderID, Self.UserID);
            var list = directory.Children.ConvertAll(folder => new
            {
                ID = folder.FolderID,
                FolderID = folder.ParentID,
                Name = folder.FolderName,
                CreateDate = folder.CreateDate.ToString(),
                Ext = "",
                LastUpdateDate = folder.LastUpdateDate.ToString(),
                Size = folder.ContentLength == 0 ? "" : folder.ContentLength.ToSizeString(),
                IsFolder = true,
                Url = ""
            }).Union(directory.Files.ConvertAll(file => new { 
                ID = file.FileID,
                FolderID = file.FolderID,
                Name = file.FileName,
                CreateDate = file.CreateDate.ToString(),
                Ext = file.Extension,
                LastUpdateDate = file.LastUpdateDate.ToString(),
                Size = file.ContentLength == 0 ? "" : file.ContentLength.ToSizeString(),
                IsFolder = false,
                Url = file.Url
            }));
            return Json(list);
        }

        [HttpPost]
        public ActionResult SaveStorage(FormCollection form) { 
        
        }
        #endregion
    }
}
